/**
 * 
 */
package andersonb.spring.social.config;

import javax.inject.Inject;

import org.springframework.social.twitter.api.Twitter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Controlador MVC para o Exemplo Twitter.
 * 
 * @author Anderson Bernardo
 */
@Controller
public class ControladorTwitter {

	@Inject
	private Twitter twitter;

	@RequestMapping("/twitter")
	public ModelAndView index() {
		ModelAndView resultado = new ModelAndView("twitter/index");
		resultado.addObject("autorizado", twitter.isAuthorized());

		if (twitter.isAuthorized()) {
			resultado.addObject("perfil", twitter.userOperations()
					.getUserProfile());
			resultado.addObject("timeline", twitter.timelineOperations()
					.getHomeTimeline());
		}

		return resultado;
	}

	@RequestMapping(value = "/twitter/enviar", method = RequestMethod.POST)
	public ModelAndView enviar(@RequestParam String texto) {
		twitter.timelineOperations().updateStatus(texto);
		return index();
	}

}
